<?php

/*
 * Snippet 1 from
 * http://www.php.net/manual/en/language.types.intro.php
 */


/*
 * Create an instance of IndentedTextWriter to hold the generated code
 */

$writer = new IndentedTextWriter();

/*
 * Create an instance of the code generator options class
 */

$options = new CodeGeneratorOptions();

/*
 * The example uses the awkward bracing style, so:
 */

$options->set_BracingStyle(BracingStyle::KR);

/*
 * both $writer and $options will be arguments to one of
 * the PHPCodeProvider code generating methods.
 * Create an instance of the PHP code provider class
 */

$provider = new PHPCodeProvider();

/*
 * Start creating the code tree.
 * The example consists of several statements that live in the default
 * namespace (ie. no namespace), which is represented by an unnamed
 * CodeNamespace instance.
 */

$defaultNamespace = new CodeNamespace();

/*
 * In the Manual exemple, $a_str, $an_int, $a_bool, is_string() and gettype()
 * are used more than once, so it is useful to create variables
 * representing them so they can be reused.
 */

$a_bool_reference = new VariableReferenceExpression('a_bool');
$a_str_reference = new VariableReferenceExpression('a_str');
$an_int_reference = new VariableReferenceExpression('an_int');

$gettype_reference = new FunctionReferenceExpression('gettype');
$is_string_reference = new FunctionReferenceExpression('is_string');

/*
 * Start adding statements to the namespace.
 * The first four statemtents are assignment statements.
 */

$statement1 = new AssignStatement(
    $a_bool_reference, new ConstantReferenceExpression('TRUE'));

/*
 * The statements will be generated in the order
 * in which they are added to the namespace.
 */

$defaultNamespace->Statements->add($statement1);

$statement2 = new AssignStatement(
    $a_str_reference, new PrimitiveExpression('foo'));

$defaultNamespace->Statements->add($statement2);

/*
 * Only single-quoted strings for now, sorry.
 */

$statement3 = new AssignStatement(
    new VariableReferenceExpression('a_str2'), 
    new PrimitiveExpression('foo'));

$defaultNamespace->Statements->add($statement3);

$statement4 = new AssignStatement(
    $an_int_reference, new PrimitiveExpression(12));
$statement4->BlankLinesAfter = 1;

$defaultNamespace->Statements->add($statement4);

/*
 * Now the two echo statements:
 */

$statement5 = new EchoStatement();
$statement5->OutputParameters->add(
    new FunctionInvokeExpression($gettype_reference));
$statement5->OutputParameters[0]->Parameters->add($a_bool_reference);

$defaultNamespace->Statements->add($statement5);

$statement6 = new EchoStatement();
$statement6->OutputParameters->add(
    new FunctionInvokeExpression($gettype_reference));
$statement6->OutputParameters[0]->Parameters->add($a_str_reference);
$statement6->BlankLinesAfter = 1;

$defaultNamespace->Statements->add($statement6);

/*
 * The two if statements follow
 */

$statement7 = new IfElseStatement(
    new FunctionInvokeExpression($is_string_reference));
$statement7->Condition->Parameters->add($an_int_reference);
$statement7->TrueStatements->add(
    new AssignStatement(
        $an_int_reference, 
        new PrimitiveExpression(4), 
        CompoundAssignmentOperator::PLUS_ASSIGN));
$statement7->BlankLinesAfter = 1;

$defaultNamespace->Statements->add($statement7);

$statement8 = new IfElseStatement(
    new FunctionInvokeExpression($is_string_reference));
$statement8->Condition->Parameters->add($a_bool_reference);
$statement8->TrueStatements->add(new EchoStatement());
$statement8->TrueStatements[0]->OutputParameters->add(
    new PrimitiveExpression('String: a_bool'));

$defaultNamespace->Statements->add($statement8);

/*
 * Generate the code
 */

$provider->generateCodeFromNamespace($defaultNamespace, $writer, $options);

/*
 * Print the generated code
 */

echo 
    '<pre>' . str_replace(
        '<br />', '', highlight_string($writer->get_String(), true)) . 
    '<pre/>';

/*
 * The output should be:
 * 
 * 
 * $a_bool = TRUE;
 * $a_str = 'foo';
 * $a_str2 = 'foo';
 * $an_int = 12;
 *
 * echo gettype($a_bool);
 * echo gettype($a_str);
 *
 * if(is_string($an_int)){
 *     $an_int += 4;
 * }
 *
 * if(is_string($a_bool)){
 *     echo 'String: a_bool';
 * }
 *
 */
?>
